package Pluto;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import Pluto.DBConnection;
import Pluto.function;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class blog extends ActionSupport{
	private String title;
	private String author;
	private String ref_name;
	private String content;
	private String time;
	private int click;
	private String url;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getRefName() {
		return ref_name;
	}

	public void setRefName(String ref_name) {
		this.ref_name = ref_name;
	}
	
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String execute() throws Exception {
		ServletActionContext.getResponse().setCharacterEncoding("GB2312");
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		ServletActionContext.getResponse().setHeader("Pragma", "No-cache");
		ServletActionContext.getResponse().setHeader("Cache-Control",
				"no-cache");
		ServletActionContext.getResponse().setDateHeader("Expires", 0);
		String userName = ServletActionContext.getContext().getSession().get(
				"PlutoUser").toString();

		if (function.isInvalid(title)) {
			out.println(function.PlutoJump("请填入日志标题！", "blog.jsp"));
		}
		if (function.isInvalid(content)) {
			out.println(function.PlutoJump("请填入日志内容！", "blog.jsp"));
		}
		DBConnection conn = new DBConnection();
		//ResultSet rs = conn--后续可以添加  @好友的功能 通过to实现
				//.executeQuery("select id,name from user where name='" + to
						//+ "'");
		//if (rs.next()) {
			//String to_name = rs.getString("name");
			//if (userName.equals(to_name)) {
				//out
						//.println(function.PlutoJump("对不起，不能给自己发短信息！",
								//"message.jsp"));
			//} else {
				long time = new Date().getTime();
				//int to_id = rs.getInt("id");
				if (conn
						.execute("insert into blog (`title`,`author`,`ref_name`,`content`,`time`,`click`) values('"
								+title
								+"','"
								+ userName
								+ "','"
								+"@-1"
								+"','"
								+ content
								+ "','" 
								+time + 
								"',1)" )) {
					out.println(function.PlutoJump("发表成功！", "blog.jsp"));
				} else {
					out.println(function.PlutoJump("发表失败！", "blog.jsp"));
				//}
			}
		//} else {
			//out.println(function.PlutoJump("用户不存在！", "message.jsp"));
		//}
		return null;
	}
}

